package com.example.shopping.service;

import com.example.shopping.entity.Admin;
import com.example.shopping.entity.User;
import com.example.shopping.mapper.AdminMapper;
import com.example.shopping.mapper.UserMapper;
import com.example.shopping.service.login.LoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private MinioService minioService;

    @Autowired
    LoginStrategy loginStrategy;

    //登录
    public User login(String account, String password) {
        System.out.println("适配者模式实现登录");
        return loginStrategy.login(account, password);

    }

    //注册
    public boolean insertUser(String account, String password, String telephone, String email, String avatar) throws Exception {
        //获取同一个账号名称的用户，看是否存在于数据库，如果存在，返回0
        User curUser = userMapper.getUserByAccount(account);
        if (curUser != null) {
            return false;//该用户已经注册过了
        }
        //构建一个新用户
        User newUser = new User();
        newUser.setAccount(account);
        //设置密码md5加密
        newUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        newUser.setTelphone(telephone);
        newUser.setEmail(email);
        //设置初始数据
        newUser.setPoints(0);
        newUser.setMoney(BigDecimal.valueOf(0.0));
        newUser.setRegTime(LocalDateTime.now());
        newUser.setStatus("0");
        newUser.setAvatar(avatar);
        System.out.println(newUser);
        return userMapper.insert(newUser) > 0;//返回添加用户的数量
    }

    //保存头像
    /*
     * 接收文件，上传到minio，返回图片链接
     * */
    public String saveAvatar(MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return null;
        }
        String url = minioService.uploadFile(file);
        // 返回相对路径，前端可以直接访问
        return url;
    }

    /**
     * 冻结用户
     */
    @Transactional
    public boolean freezeUser(Integer id) {
        return userMapper.updateUserStatus(id, "2") > 0;
    }

    /**
     * 解冻用户
     */
    @Transactional
    public boolean unfreezeUser(Integer id) {
        return userMapper.updateUserStatus(id, "0") > 0;
    }

    public boolean logout(Integer id) {
        return userMapper.updateUserStatus(id, "0") > 0;
    }

    /**
     * 加入黑名单
     */
    @Transactional
    public boolean addToBlacklist(Integer id) {
        return userMapper.updateBlacklistStatus(id, "3") > 0;
    }

    /**
     * 移除黑名单
     */
    @Transactional
    public boolean removeFromBlacklist(Integer id) {
        return userMapper.updateBlacklistStatus(id, "0") > 0;
    }

    /**
     * 删除用户
     */
    @Transactional
    public boolean deleteUser(Integer id) {
        return userMapper.deleteUser(id) > 0;
    }


    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
}
